﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace ISAMonitor.RESTTool
{
    /// <summary>
    /// REST方式指令工具
    /// </summary>
    public class RESTCommand
    {
        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="URL">所在URL</param>
        /// <param name="postinfo">指令信息</param>
        /// <returns>返回的JSON结果</returns>
        public static string SendCommand(string URL, string postinfo)
        {
            return htmlGetter(URL, postinfo, "UTF-8", true, null, null);
        }


        public static string htmlGetter(string URL, string post, string codename, bool isPost, string[] headinfo, string Referer)
        {

            Encoding myEncoding = Encoding.GetEncoding(codename);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
            request.ServicePoint.Expect100Continue = false;

            byte[] byteData = myEncoding.GetBytes(post);
            if (isPost)
            {
                request.Method = "POST";
            }
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteData.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            if (Referer != null)
            {
                request.Referer = Referer;
            }

            if (headinfo != null)
            {
                for (int i = 0; i < headinfo.Length; i++)
                {
                    request.Headers.Add(headinfo[i]);
                }
            }

            //if (_isDebug)
            //{
            //    WebProxy myproxy = new WebProxy("127.0.0.1", 8888);
            //    request.Proxy = myproxy;
            //}

            if (isPost)
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(byteData, 0, byteData.Length);
                }
            }

            try
            {
                //处理响应  
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader sr2 = new StreamReader(response.GetResponseStream(), myEncoding))
                    {
                        string rhtml = sr2.ReadToEnd();
                        return rhtml;
                    }
                }
            }
            catch (WebException ex)
            {
                throw;
            }
        }
    }

    /// <summary>
    /// 序列化工具
    /// </summary>
    public class SerializationTool
    {
        /// <summary>
        /// 从Jsontext获取实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="JsonText"></param>
        /// <returns></returns>
        public static T GetObj<T>(string JsonText)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonText));
            T obj = (T)ds.ReadObject(ms);
            ms.Close();

            return obj;
        }

        /// <summary>
        /// 从实体转换到JsonText
        /// </summary>
        /// <param name="myobj">实体实例</param>
        /// <returns></returns>
        public static string ParseObj(object myobj)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(myobj.GetType());
            MemoryStream ms2 = new MemoryStream();
            ds.WriteObject(ms2, myobj);
            string JSONString = Encoding.UTF8.GetString(ms2.ToArray());
            ms2.Close();

            return JSONString;
        }
    }
}
